var registerEnv = {	
	// LIMITS and ERRORS defined in register.html.
	LIMITS: LIMITS,
	MESSAGES: MESSAGES,
	
	// errors global variable defined in register.html.
	server_validation_errors: errors,
	validator: null,
	
	// If page is currently in the middle of processing a registration request.
	busyRegistering: false
}


function createClientSideValidation() {
    var limits = registerEnv.LIMITS;
    var messages = registerEnv.MESSAGES;
    
	// Method validating if value consists only of word characters.
	$.validator.addMethod("word", function(value, element) {
		var re = new RegExp(/^\w*$/);
		return re.test(value);
	});
	
	// Form validation.
	registerEnv.validator = $("#register form").validate({		
		rules: {
			"username": {
				required:    true,
				rangelength: [limits["username_min_length"],
				              limits["username_max_length"]],
				word:        true
			},
			"password": {
				required:    true,
				rangelength: [limits["password_min_length"],
				              limits["password_max_length"]],
				word:        true
			},
			"password-repeat": {
				required: true,
				equalTo:  "#password"
			},
			"email": {
				required:    limits["email_required"],
				rangelength: [limits["email_min_length"], limits["email_max_length"]],
				email:       true
			},
			"name": {
				required:    limits["name_required"],
				rangelength: [limits["name_min_length"], limits["name_max_length"]]
			},
			"surname": {
				required:    limits["surname_required"],
				rangelength: [limits["surname_min_length"], limits["surname_max_length"]]
			},
			"date-of-birth": {
				required: registerEnv.LIMITS["date_of_birth_required"],
				dateISO:  true
			}
		},
		
		messages: {
		    "username": {
		        required:    messages["username_not_defined"],
		        rangelength: messages["username_invalid_length"],
		        word:        messages["username_invalid_format"]
		    },
		    "password": {
		        required:    messages["password_not_defined"],
		        rangelength: messages["password_invalid_length"],
		        word:        messages["password_invalid_format"]
		    },
		    "password-repeat": {
		        required: messages["password_repeat_not_defined"],
		        equalTo:  messages["password_repeat_equal_to"]
		    },
		    "email": {
		        required:    messages["email_not_defined"],
		        rangelength: messages["email_invalid_length"],
		        email:       messages["email_invalid_format"]
		    },
		    "name": {
		        required:    messages["name_not_defined"],
		        rangelength: messages["name_invalid_length"]
		    },
		    "surname": {
		        required:    messages["surname_not_defined"],
		        rangelength: messages["surname_invalid_length"]
		    },
		    "date-of-birth": {
		        required: messages["date_of_birth_not_defined"],
		        dateISO:  messages["date_of_birth_invalid"],
		        date:  messages["date_of_birth_invalid"]
		    }
		},
		
		errorClass: "error"
	});
}


function showServerValidationErrorMessages(errors) {
    var messages = registerEnv.MESSAGES;
	var errorsToShow = { };
	
	if (errors["username_not_unique"])
        errorsToShow["username"] = messages["username_not_unique"];
	if (errors["username_not_defined"])
		errorsToShow["username"] = messages["username_not_defined"];
	if (errors["username_invalid_length"])
		errorsToShow["username"] =  messages["username_invalid_length"]
	if (errors["username_invalid_format"])
		errorsToShow["username"] = messages["username_invalid_format"];
	
	if (errors["password_not_defined"])
		errorsToShow["password"] = messages["password_not_defined"];
	if (errors["password_invalid_length"])
		errorsToShow["password"] = messages["password_invalid_length"];
	if (errors["password_invalid_format"])
		errorsToShow["password"] = messages["password_invalid_format"];
	
	if (errors["email_not_defined"])
		errorsToShow["email"] = messages["email_not_defined"];
	if (errors["email_not_unique"])
		errorsToShow["email"] = messages["email_not_unique"];
	if (errors["email_invalid_length"])
		errorsToShow["email"] = messages["email_invalid_length"];
	if (errors["email_invalid_format"])
		errorsToShow["email"] = messages["email_invalid_format"];
	
	if (errors["name_not_defined"])
		errorsToShow["name"] = messages["name_not_defined"];	
	if (errors["name_invalid_length"])
		errorsToShow["name"] = messages["name_invalid_length"];
	
	if (errors["surname_not_defined"])
		errorsToShow["surname"] = messages["surname_not_defined"];
	if (errors["surname_invalid_length"])
		errorsToShow["surname"] = messages["surname_invalid_length"];
	
	if (errors["date_of_birth_not_defined"])
		errorsToShow["date-of-birth"] = messages["date_of_birth_not_defined"];	
	if (errors["date_of_birth_invalid"])
		errorsToShow["date-of-birth"] = messages["date_of_birth_invalid"];
	
	registerEnv.validator.showErrors(errorsToShow);
}


function disableMultipleFormSubmission() {
	$("#register form").submit(function() {
		// We must check the form's validation, because submit event is
		// triggerred before validation. And so so we don't want to disable
		// submit button if the form is invalid, because the submission
		// will be cancelled anyway.
		if (registerEnv.validator.form()) {
			$(this).unbind("submit").submit(function() {
				return false;
			});
			
			$("#submit").val("Wait...");
		}
		else {
			return false;
		}
	});
}


$(document).ready(function() {
	createClientSideValidation();
	showServerValidationErrorMessages(registerEnv.server_validation_errors);
	disableMultipleFormSubmission();
	
	// Calendar popup when chosing date of birth.
	$('#date-of-birth').datepicker({ dateFormat: "yy-mm-dd", changeMonth: true, stepMonths: 12 });
});
